package com.shaoshaohuo.app.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.adapter.AddressAdapter;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.entity.Address;
import com.shaoshaohuo.app.entity.AddressEntity;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.view.TopbarView;

import java.util.ArrayList;
import java.util.List;

/**
 * 常用收货地址
 * 
 * 如果需要选择 应该 startActivityForResult() intent.putExtra(ExtraName.isForResult,
 * true); 在 onActivityResult（） 中获取 ExtraName.address Address 对象
 * 
 * @author cao
 *
 */
public class AddressManagerActivity extends BaseActivity implements OnItemClickListener, OnClickListener {

	private TopbarView mTopbarView;
	private ListView listView;
	private List<Address> list = new ArrayList<>();
	private AddressAdapter addressAdapter;
	private boolean isSelect;
	public static final int FLAG_EDIT_ADDRESS = 251;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_address_manager);
		Intent intent = getIntent();
		isSelect = intent.getBooleanExtra(ExtraName.isForResult, false);

		initView();
		setUpView();
		requestData();
	}

	@Override
	protected void onResume() {
		requestData();
		super.onResume();
	}

	private void initView() {
		mTopbarView = (TopbarView) this.findViewById(R.id.topbar);
		listView = (ListView) findViewById(R.id.listview);
	}

	private void setUpView() {
		mTopbarView.setCenterText("常用收货地址");
		mTopbarView.setLeftView(true, true);
		mTopbarView.setRightText("新增");
		mTopbarView.setRightClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(AddressManagerActivity.this, AddressEditActivity.class);
				if (addressAdapter != null && addressAdapter.defaultAddress == null)
					intent.putExtra("setDefault", true);

				startActivityForResult(intent, FLAG_EDIT_ADDRESS);
			}
		});

		if (isSelect)
			listView.setOnItemClickListener(this);
	}

	public void requestData() {
		startLoadingDialog();
		RequestService.getInstance().getOftenAddress(this, AddressEntity.class, new RequestListener() {
			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				dismissLoadingDialog();
				if (resultData.isOk()) {
					AddressEntity addressEntity = (AddressEntity) resultData;
					if (addressEntity.getData() != null) {
						list = addressEntity.getData();
						addressAdapter = new AddressAdapter(AddressManagerActivity.this, list);
						listView.setAdapter(addressAdapter);

					}
				}

			}

			@Override
			public void onFailed(int requestCode, Exception error, String msg) {
				dismissLoadingDialog();
				showToast(msg);
			}
		});
	}

	/**
	 * 当前状态时 选择地址 时 需要相应点击事件 返回 当前点击的地址
	 */
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Intent intent = new Intent();
		Address address = list.get(position);
		intent.putExtra(ExtraName.address, address);
		setResult(Activity.RESULT_OK, intent);
		finish();
	}

	@Override
	public void onClick(View v) {

	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == FLAG_EDIT_ADDRESS) {
			if (resultCode == RESULT_OK) {
				requestData();
			}
		}
	}
}
